/**
 * @projectName demo-master
 * @fileName UserServiceImpl.java
 * @packageName org.example.nacos.data.service.impl
 * @author xaoyaoyao
 * @date 2021/3/24 14:10
 * @version V1.0
 * @copyright (c) 2021, xaoyaoyao@aliyun.com All Rights Reserved.
 */
package org.example.nacos.data.service.impl;

import org.example.nacos.data.dao.UserRepository;
import org.example.nacos.data.model.User;
import org.example.nacos.data.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * @author xaoyaoyao
 * @className UserServiceImpl
 * @description
 * @date 2021/3/24 14:10
 */
@Service
@RefreshScope
public class UserServiceImpl implements UserService {

	private static final Logger LOGGER = LoggerFactory.getLogger(UserServiceImpl.class);

	private final UserRepository userRepository;

	private final RedisTemplate redisTemplate;

	@Value("${app.user.cache}")
	private boolean cache;

	@Autowired
	public UserServiceImpl(UserRepository userRepository, RedisTemplate redisTemplate) {
		this.userRepository = userRepository;
		this.redisTemplate = redisTemplate;
	}

	@Override
	public User findById(Long id) {
		LOGGER.info("cache: {}", cache);
		if (cache) {
			Object obj = redisTemplate.opsForValue().get(key(id));
			if (obj != null) {
				LOGGER.info("get user from cache, id: {}", id);
				return (User) obj;
			}
		}

		User user = userRepository.findById(id).orElse(null);
		if (user != null) {
			if (cache) {
				LOGGER.info("set cache for user, id: {}", id);
				redisTemplate.opsForValue().set(key(id), user);
			}
		}
		return user;
	}

	private String key(Long id) {
		return String.format("nacos-spring-cloud-config-multi-data-ids-example:user:%d", id);
	}

}
